import { Prefab, instantiate, assetManager, Node, EventTouch } from 'cc';
import { ccclass, ccmodifier } from '../decorators';
import { BaseView } from './BaseView';
import { app } from '../application';
import { BaseBigLoading } from './BaseBigLoading';

/**
 * - 作者: DoooReyn
 * - 日期: 2023.10.31
 * - 名称: BigLoading
 * - 描述: 视图-大场景资源加载层
 */
@ccclass()
@ccmodifier('BigLoading')
export class BigLoading extends BaseView {
    /** 当前加载页预制体 */
    protected _loading: Prefab | null;

    /** 当前加载页 */
    protected _page: BaseBigLoading | null;

    /** 正在加载中 */
    get isLoading() {
        return this._loading !== null;
    }

    protected onEnable(): void {
        this.node.on(Node.EventType.TOUCH_START, this.onTouchScreen, this);
        this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchScreen, this);
        this.node.on(Node.EventType.TOUCH_END, this.onTouchScreen, this);
        this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchScreen, this);
    }

    protected onDisable(): void {
        this.node.off(Node.EventType.TOUCH_START, this.onTouchScreen, this);
        this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchScreen, this);
        this.node.off(Node.EventType.TOUCH_END, this.onTouchScreen, this);
        this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchScreen, this);
    }

    /** 屏幕被点击时调用 */
    onTouchScreen(e: EventTouch) {
        e.preventSwallow = false;
        e.propagationStopped = true;
        e.propagationImmediateStopped = true;
        this.logger.debug('禁止触摸继续传递');
    }

    /**
     * 显示加载页
     * @param path BigLoading 资源路径
     * @param bundle 所属资源包
     */
    async showLoading(path: string, bundle: string = 'resources') {
        const prefab = await app.resLoader.loadOne({ bundle, path, type: Prefab });
        if (prefab && prefab.data.getComponent(BaseBigLoading)) {
            this._loading = prefab;
            const loading = instantiate(prefab);
            this._page = loading.getComponent(BaseBigLoading);
            this.node.addChild(loading);
            this.show();
        }
    }

    /** 移除加载页 */
    async removeLoading() {
        this.node.destroyAllChildren();
        this._loading && assetManager.releaseAsset(this._loading);
        this._loading = null;
        this._page = null;
        await this.hide();
    }

    /** 加载开始时调用 */
    async onLoadStart() {
        await this._page?.onLoadStart();
    }

    /** 
     * 加载进度更新时调用
     * @param progress 当前量
     * @param total 总量
     */
    onLoadProgress(progress: number, total: number) {
        this._page?.onLoadProgress(progress, total);
    }

    /** 
     * 加载出现错误时调用 
     * @param err 错误信息
     */
    onLoadError(err: any) {
        this._page?.onLoadError(err);
    }

    /** 加载完成时调用 */
    async onLoadEnd() {
        await this._page?.onLoadEnd();
    }

    /** 初始化 */
    public async init() {
        this.logger.debug('初始化');
        await this.hide();
    }

    /** 反初始化 */
    async exit() {
        this.logger.debug('反初始化');
        await this.removeLoading();
    }
}
